#include "stdafx.h"
#include "SqlNetStorage.h"
using namespace Dimine::Core;

dmDbSqlNetStorageCS::dmDbSqlNetStorageCS()
{
	m_native=new CNetTableStorager();
}


bool dmDbSqlNetStorageCS::SetAdoConnection(STRINGCS username,STRINGCS connectionString)
{
	VALIDATE_NATIVE
	TCHAR* connstr=ManagedString2UnmanagedString(username);
	CAdoConnection* conn=new CAdoConnection();
	
	if(!conn) return false;

	int res=conn->Open(connstr);
	if(!res)
	{
		if(conn->IsOpen())
			conn->Close();
		delete conn;
	}
	String2Char(username)
	return m_native->SetAdoConnection(fn,conn)==DM::eOk;
}
bool dmDbSqlNetStorageCS::Load(long fileid,STRINGCS strTimeStamp,bool bIsLastest)
{
	VALIDATE_NATIVE
	String2Char(strTimeStamp)
	return m_native->Load(fileid,fn,bIsLastest)==DM::eOk;
}

bool dmDbSqlNetStorageCS::Load(STRINGCS filename)
{
	VALIDATE_NATIVE
	String2Char(filename)
	return m_native->Load(fn);
}
bool dmDbSqlNetStorageCS::Load(STRINGCS projectName,STRINGCS foldName,STRINGCS fileName)
{
	VALIDATE_NATIVE
	String2Char(projectName)
	TCHAR* chpname=ManagedString2UnmanagedString(projectName);
	char* fnpname=toChar(chpname);

	TCHAR* chfname=ManagedString2UnmanagedString(foldName);
	char* fnfname=toChar(chfname);

	TCHAR* chfile=ManagedString2UnmanagedString(fileName);
	char* fnfile=toChar(chfile);

	return m_native->Load(fnpname,fnfname,fnfile);
}

bool dmDbSqlNetStorageCS::Save(STRINGCS filename,bool isNew,long foldid,long fileid)
{
	VALIDATE_NATIVE
	String2Char(filename)
	return m_native->Save(fn,isNew,foldid,fileid)==DM::eOk;
}

bool dmDbSqlNetStorageCS::Save(STRINGCS projectName,STRINGCS foldName,STRINGCS filename)
{
	VALIDATE_NATIVE
	String2Char(projectName)
	TCHAR* chpname=ManagedString2UnmanagedString(projectName);
	char* fnpname=toChar(chpname);

	TCHAR* chfname=ManagedString2UnmanagedString(foldName);
	char* fnfname=toChar(chfname);

	TCHAR* chfile=ManagedString2UnmanagedString(filename);
	char* fnfile=toChar(chfile);

	return m_native->Save(fnpname,fnfname,fnfile);
}
bool dmDbSqlNetStorageCS::Save(STRINGCS filename,long version)
{
	VALIDATE_NATIVE
	String2Char(filename)
	return m_native->Save(fn,version);
}

bool dmDbSqlNetStorageCS::SaveTable(STRINGCS username,int nProFolID,STRINGCS strFileName,dmDataTableCS^ table IN,long nFileID)
{
	VALIDATE_NATIVE
	String2Char(username)
	return m_native->SaveNetTable(fn,nProFolID,toChar(ManagedString2UnmanagedString(strFileName)),table->Native,nFileID);
}
bool dmDbSqlNetStorageCS::LoadTable(int nFileID,dmDataTableCS^% table,dmDataBaseInfoCS^% dbinfo,STRINGCS strTimeStamp,bool bIsLastest)
{
	VALIDATE_NATIVE
	String2Char(strTimeStamp)
	CDataTable* dt=table->Native;
	CNetTableStorager::_dbInfo* info=dbinfo->Native;
	bool res=m_native->LoadNetTable(nFileID,dt,*info,fn,bIsLastest);
	table->Native=dt;
	dbinfo->Native=info;
	return res;
}
bool dmDbSqlNetStorageCS::DelTable(dmTableInfoCS^% tbInfo,dmDataBaseInfoCS^% dbinfo)
{
	VALIDATE_NATIVE
	CNetTableStorager::_tbInfo* tb=tbInfo->Native;
	CNetTableStorager::_dbInfo* db=dbinfo->Native;
	bool res=m_native->DelNetTable(*tb,*db);
	tbInfo->Native=tb;
	dbinfo->Native=db;
	return res;
}

bool dmDbSqlNetStorageCS::SaveRecords(long nFileID,STRINGCS strCurrentTime,bool isNew)
{
	VALIDATE_NATIVE
	String2Char(strCurrentTime)
	return m_native->SaveRecords(nFileID,fn,isNew);
}
bool dmDbSqlNetStorageCS::SaveFields(long nFileID,STRINGCS strCurrentTime,bool isNew)
{
	VALIDATE_NATIVE
	String2Char(strCurrentTime)
	return m_native->SaveFields(nFileID,fn,isNew);
}
bool dmDbSqlNetStorageCS::LoadFields(long lFileID,STRINGCS strTimeStamp,bool bIsLastest)
{
	VALIDATE_NATIVE
	String2Char(strTimeStamp)
	return m_native->SaveFields(lFileID,fn,bIsLastest);
}
